import mysql.connector
from server.views.server_config.mysql import mysql_connect_config
from django.shortcuts import render_to_response,HttpResponse
import simplejson
import json

def login(request):
    print request.POST
    if request.POST:
        user_name=(request.POST['user_name'])
        user_passwd=(request.POST['user_passwd'])
        print user_name,type(user_name)
        print user_passwd,type(user_passwd)
        try:
            mysql_conn = mysql.connector.connect(**mysql_connect_config)
            mysql_cursor = mysql_conn.cursor()
            mysql_query=('select * from user where user_name = "%s"' % user_name)
            mysql_cursor.execute(mysql_query)
            # print mysql_cursor.fetchall()
            mysql_fetch=mysql_cursor.fetchall()
            print mysql_fetch
            # print type(mysql_fetch[0][1]),mysql_fetch[0][1]
            # print type(user_passwd),user_passwd
            if (mysql_fetch):
                if (user_passwd == mysql_fetch[0][1]):
                    # return HttpResponse(simplejson.dumps('0'),mimetype='application/javascript'
                    return HttpResponse(json.dumps([0]),content_type='application/json')
                else:
                    return HttpResponse(json.dumps([1]),content_type='application/json')
            else:
                return HttpResponse(json.dumps([2]),content_type='application/json')
        except:
            return HttpResponse(json.dumps([3]),content_type='application/json')
    # return HttpResponse('Hello world')

